function fig = PlotResultsSEP(outputOfAnalyze, subjectNr, electrode, period)
%PlotResultsSEP Plot results per subject, electrode and period
% Normalize and merge blocks, display all 4 peaks.
%     subjectNr: int
%     electrode: string
%     period: int

subject = outputOfAnalyze.subjects(subjectNr);
blocks = outputOfAnalyze.results(subjectNr).period{period}.block;
X = []; D = [];
for b = 1:length(blocks)
    if isempty(blocks{b}); continue; end;
    load(blocks{b});
    data = zeros(8,length(results.(electrode).trial)); % N1time, N1value, P1... ... ...
    for t = 1:length(results.(electrode).trial)
        data(1,t) = results.(electrode).trial(t).N1.time;
        data(2,t) = results.(electrode).trial(t).N1.value;
        data(3,t) = results.(electrode).trial(t).P1.time;
        data(4,t) = results.(electrode).trial(t).P1.value;
        data(5,t) = results.(electrode).trial(t).N2.time;
        data(6,t) = results.(electrode).trial(t).N2.value;
        data(7,t) = results.(electrode).trial(t).P2.time;
        data(8,t) = results.(electrode).trial(t).P2.value;
    end
    [Db Xb] = NormalizeBlock(data);
    D = [D Db];
    X = [X Xb];
end
[sy si] = sort(X);
D = D(:,si);
X = X(si);

fig = figure;
subplot(4,1,1); 
DoPlot(D(1,:), D(2,:), X);
ylabel N1
title ([subject.Name ' - Period ' num2str(period) ' - Electrode ' electrode]);
legend('latency', 'value')

subplot(4,1,2);
DoPlot(D(3,:), D(4,:), X);
ylabel P1

subplot(4,1,3);
DoPlot(D(5,:), D(6,:), X);
ylabel N2

subplot(4,1,4);
DoPlot(D(7,:), D(8,:), X);
ylabel P2

end


function DoPlot(T, V, X)
xlim([0 1])
hold on;
window = 20;
if window > length(X)
    window = length(X);
end
plot(X, fastsmooth(T,window, 3, 1), 'r');
plot(X, fastsmooth(V, window, 3, 1), 'b');
end